package com.demo.springbootfileupload.service.impl;


import com.demo.springbootfileupload.config.MinIOConfig;
import com.demo.springbootfileupload.service.MinIOService;
import com.demo.springbootfileupload.utils.MinIOUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Hanson
 * @date 2022/9/7 16:28
 */
@Service
@Slf4j
public class MinIOServiceImpl implements MinIOService {
    @Autowired
    private MinIOUtils minIOUtils;

    @Autowired
    private MinIOConfig minioConfig;

    String MinIOPublicIP = "http://139.224.83.131:9000/";

    /**
     * 判断 bucket是否存在
     *
     * @param bucketName
     * @return
     */
    @Override
    public boolean bucketExists(String bucketName) {
        return minIOUtils.bucketExists(bucketName);
    }

    /**
     * 创建 bucket
     *
     * @param bucketName
     */
    @Override
    public void makeBucket(String bucketName) {
        minIOUtils.makeBucket(bucketName);
    }

    /**
     * 文件上传
     *
     * @param bucketName
     * @param objectName
     * @param filename
     */
    @Override
    public void putObject(String bucketName, String objectName, String filename) {
        minIOUtils.putObject(bucketName, objectName, filename);
    }


    @Override
    public void putObject(String bucketName, String objectName, InputStream stream, String contentType) {
        minIOUtils.putObject(bucketName, objectName, stream, contentType);
    }

    /**
     * 文件上传
     *
     * @param bucketName
     * @param multipartFile
     */
    @Override
    public void putObject(String bucketName, MultipartFile multipartFile, String filename) {
        minIOUtils.putObject(bucketName, multipartFile, filename);
    }

    /**
     * 删除文件
     * @param bucketName
     * @param filePath
     */
    @Override
    public boolean removeObject(String bucketName,String filePath) {
        return minIOUtils.removeObject(bucketName,filePath);
    }

    /**
     * 下载文件
     * @param bucketName
     * @param filePath
     * @param originalName
     * @param response
     * @return
     */
    @Override
    public boolean downloadFile(String bucketName, String filePath, String originalName, HttpServletResponse response) {
        return minIOUtils.downloadFile(bucketName,filePath, originalName, response);
    }

    /**
     * 获取文件路径
     * @param bucketName
     * @param objectName
     * @return
     */
    @Override
    public String getObjectUrl(String bucketName,String objectName) {
        return minIOUtils.getObjectUrl(bucketName,objectName);
    }

    /**
     * @param file 升级文件
     * @return
     * @throws Exception
     */
    @Override
    public String uploadUpgradeFile(MultipartFile file) throws Exception {

        // 判断文件是否存在
        if (file == null) {
            throw new Exception("未获取文件");
        }
        // 获取文件对象原名
        String originalFilename = file.getOriginalFilename();

        // 获取桶名
        String bucketName = minioConfig.getBucketName();
        // 判断请求中是否有bucketName，没有则调用配置中的bucketName
        // bucketName = !StringUtils.isEmpty(bucketName) ? bucketName : minioConfig.getBucketName();
        // 判断这个bucketName在文件服务器中是否存在，不存在则创建这个桶
        if (!bucketExists(bucketName)) {
            //makeBucket(bucketName); // 没有桶则创建桶
            log.error("桶不存在异常:{}", bucketName);
            throw new Exception("桶不存在");
        }
        // 根据业务设计，设置存储路径，按天创建目录,按设备id+版本号+原来名称给文件命名  /不要放在format
        String objectName = new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + originalFilename;

        putObject(bucketName, file, objectName);
        //log.info("文件格式为:{},文件原名称为:{},文件存储得桶为:{},文件对象路径为:{}", file.getContentType(), fileName, bucketName, objectName);
        String fileUrl = getObjectUrl(bucketName, objectName);
        // 按照minIO服务器公网ip + 桶名封装路径
        String downloadUrl = MinIOPublicIP +  bucketName + "/" + extractString(fileUrl);
        log.info(originalFilename + "：文件的上传成功，下载路径为：{}",downloadUrl);

        return downloadUrl;
    }

    /**
     * 按第三个"/"截取字符串
     * @param s
     * @return
     */
    public String extractString(String s){

        for(int i = 0; i < 4; i++){
            s = s.substring(s.indexOf("/")+1);
        }
        return s;
    }

}
